package cn.com.gridinfo.sooc.core.my.note.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.course.classes.activity.CourseDetailActivity;
import cn.com.gridinfo.sooc.core.my.commen.CodeConst;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.core.my.note.adapter.NoteDetailListAdapter;
import cn.com.gridinfo.sooc.core.my.note.bean.MyNoteDetail;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 笔记详情
 * Created by 刘晓晨 on 2015/12/22.
 */
public class NoteDetailActivity extends BaseSoocActivity {
    @InjectView(R.id.course)
    RelativeLayout course;// 点击跳转到课程详情
    @InjectView(R.id.course_main_list)
    UltimateRecyclerView noteDetailList;// 列表显示
    MyDao dao;
    NoteDetailListAdapter noteDetailListAdapter;//适配器
    @InjectView(R.id.kcmc)
    TextView textKcmc;
    private String kcmc;//拿到传来的课程名称
    private String kcid;//拿到传来的课程id
    int pageNo = 1;
    int pageSize = 10;
    int maxLastPositon;//数据最后位置
    int positionNo;//要删除的笔记

    LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_note_detail);
        ButterKnife.inject(this);
        getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.MATCH_PARENT);
        dao = new MyDao(this);
        kcmc = getIntent().getStringExtra("kcmc");//拿到传来的课程名称
        kcid = getIntent().getStringExtra("kcid");//拿到传来的课程id
        Log.d("课程名称" + kcmc);
        Log.d("课程ID" + kcid);
        textKcmc.setText(kcmc);
        dao.myNoteDetail(kcid, pageNo, CodeConst.NOTEDETAIL_FIRST);
        linearLayoutManager = new LinearLayoutManager(this);
        noteDetailList.setLayoutManager(linearLayoutManager);
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "笔记详情";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //点击课程分类
    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        Intent intent = new Intent();
        intent.putExtra("size", noteDetailListAdapter.getListCount());
        Log.d("size=" + noteDetailListAdapter.getListCount());
        setResult(RESULT_OK, intent);
        finish();
    }

    // 请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        try {
            if (dao.status.equals("0")) {
                //课程首次请求
                switch (requestCode) {
                    case CodeConst.NOTEDETAIL_FIRST:
                        initNoteDetail();
                        break;
                    case CodeConst.NOTEDETAIL_PUSHDOWN:
                        noteDetailList.setRefreshing(false);
                        if (dao.getMyNoteDetailList() != null && dao.getMyNoteDetailList().isEmpty()) {
                            noteDetailList.hideEmptyView();
                        } else {
                            //无数据处理
                            noteDetailList.showEmptyView();
                        }
                        //刷新数据
                        noteDetailListAdapter.refresh(dao.getMyNoteDetailList());
                        if (dao.getMyNoteDetailList().size() > (pageSize - 1))
                            //数据大于每页显示的条数，则重新开启加载更多
                            noteDetailListAdapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不显示加载更多
                            noteDetailListAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                    //课程翻页
                    case CodeConst.NOTEDETAIL_PULLUP:
                        if (dao.getMyNoteDetailList() != null && !dao.getMyNoteDetailList().isEmpty()) {
                            //加载更多
                            noteDetailListAdapter.loadmore(dao.getMyNoteDetailList());
                        } else {
                            //无数据则不显示下一页
                            noteDetailListAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                    case CodeConst.DELETENOTE://删除笔记
                        MessageUtils.showRightImageShortToast(this, dao.statusCode);
                        noteDetailListAdapter.delete(positionNo);
                        //判断是否数据没有更多了 有更多,删除数据当前页不满 pageSize, 则刷新加载当前页,没有更多数据,则不去加载
                        if(noteDetailListAdapter.getIsLoadMore()) {
                            if (noteDetailListAdapter.getListCount() < pageSize) {
                                //下拉刷新  账户明细数据
                                pageNo = 1;
                                dao.myNoteDetail(kcid, pageNo, CodeConst.NOTEDETAIL_PUSHDOWN);
                                linearLayoutManager.scrollToPosition(0);
                            }
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.statusCode);
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    //加载
    private void initNoteDetail() {
        noteDetailListAdapter = new NoteDetailListAdapter(dao.getMyNoteDetailList(), this);
        noteDetailList.setAdapter(noteDetailListAdapter);
        noteDetailList.enableLoadmore();
        noteDetailList.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                //下拉
                pageNo = 1;
                dao.myNoteDetail(kcid, pageNo, CodeConst.NOTEDETAIL_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);

            }
        });
        noteDetailList.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.myNoteDetail(kcid, ++pageNo, CodeConst.NOTEDETAIL_PULLUP);
                } else {
                    noteDetailListAdapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);

                }
            }
        });
        //适配器点击事件
        noteDetailListAdapter.setOnItemLongClickLitener(new NoteDetailListAdapter.OnItemLongClickListener() {
            @Override
            public void OnItemClick(View view, final MyNoteDetail myNoteDetail, int position) {
                positionNo = position;
                new MaterialDialog.Builder(NoteDetailActivity.this)
                        .items(R.array.del_note)
                        .backgroundColor(getResources().getColor(R.color.white))
                        .contentColor(getResources().getColor(R.color.color_tv333))
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                //删除笔记
                                dao.deleteNote(myNoteDetail.getBjid());
                            }
                        })
                        .show();
            }
        });

    }

    //点击顶部课程名字 跳转到课程详情界面
    @OnClick(R.id.course)
    void goToCourse() {
        Intent intent = new Intent(this, CourseDetailActivity.class);
        intent.putExtra("kcid", kcid);//课程id传出
        startActivityForResult(intent, CodeConst.BIJI_DIANZAN);
        AnimUtil.intentSlidIn(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CodeConst.BIJI_DIANZAN && resultCode == 200) {
            //下拉
            pageNo = 1;
            dao.myNoteDetail(kcid, pageNo, CodeConst.NOTEDETAIL_PUSHDOWN);
            noteDetailList.setRefreshing(false);
            noteDetailList.disableLoadmore();
            linearLayoutManager.scrollToPosition(0);
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
